// app.js
// 导入工具函数
import './utils/utils'
// 导入 http 模块
import './utils/http'
App({
  onLaunch() {
    // 打开页面时获取 token
    this.getToken('token')
    // 打开页面时获取 refreshToken
    this.getToken('refresh_token')
    wx.setStorageSync('repairitem', [{
        id: "1004",
        name: "开锁换锁"
      },
      {
        id: "1003",
        name: "管道疏通"
      },
      {
        id: "1002",
        name: "电路灯具"
      },
      {
        id: "1001",
        name: "水路卫浴"
      }
    ])

  },
  // 获取维修项目
  getrepaName(id) {
    const list = wx.getStorageSync('repairitem')
    return list.find(item => item.id = id).name
  },
  // 根据id获取房屋地址
  getAdress(id) {
    const info = wx.getStorageSync('roominfo')
    const room = info.find(item => {
      return item.roomid === id
    })
    return room.point + room.building + room.room
  },
  // 获取 token
  getToken(key) {
    wx.getStorage({
      key,
      success: ({
        data
      }) => {
        this[key] = data
      },
    })
  },
  //获取房屋信息
  getRoom() {
    return wx.getStorageSync('roominfo')
  },
  // 拼接完成信息
  getHouseList() {
    let houseList = wx.getStorageSync('roominfo')
    if (houseList.length === 0) {
      // wx.utils.toast('对不起您没有可报修的房屋')
      return wx.showToast({
        title: '对不起您没有可报修的房屋',
        icon: 'none',
        duration: 2000,
        complete: () => {
          setTimeout(() => {
            wx.navigateBack()
          }, 2000)
        },
      })
    }
    houseList = houseList.map(item => {
      return {
        id: item.roomid,
        name: item.point + item.building + item.room
      }
    })
    return houseList
  },
  // 存储保修信息
  setRepaire(obj) {
    let arr = wx.getStorageSync('repaireEvent')
    if (!arr) {
      let list = []
      list.push(obj)
      wx.setStorageSync('repaireEvent', list)
    } else {
      arr = arr.filter(item => item.id !== obj.id)
      arr.push(obj)
      wx.setStorageSync('repaireEvent', arr)
    }

  },
  setLoction(key, obj) {
    let arr = wx.getStorageSync(key)
    if (!arr) {
      let list = []
      list.push(obj)
      wx.setStorageSync(key, list)
    } else {
      arr = arr.filter(item => item.id !== obj.id)
      arr.push(obj)
      wx.setStorageSync(key, arr)
    }

  },
  // 保存 token
  setToken(token, refreshToken) {
    // 拼接参数 token
    let myToken = 'Bearer ' + token
    // 保存到本地
    wx.setStorageSync('token', myToken)
    // 保存到应用中
    this.token = myToken

    // 拼接参数 refreshToken
    let myRefreshToken = 'Bearer ' + refreshToken
    wx.setStorageSync('refresh_token', myRefreshToken)
    // 保存到应用中
    this.refreshToken = myRefreshToken
  },
})